//
//  FLLSceneViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/3.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLSceneViewController.h"
#import "FLLMacro.h"
#import "Masonry.h"
#import "FLLSceneCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FLLSceneModel.h"
#import "SceneData.h"
#import "SceneSecData.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "FLLSceneInfoViewController.h"

@interface FLLSceneViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *sceneCollectionView;
@property (nonatomic, strong) FLLSceneModel *fllSceneModel;
@property (nonatomic, strong) NSMutableArray<SceneSecData *> *secDataArray;
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, assign) BOOL isRefresh;

@end

@implementation FLLSceneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createPOSTRequest];
    [self createSceneCollectionView];
   
    
}
- (void)createPOSTRequest
{
        NSString *bodyString = [NSString stringWithFormat:@"page=%ld&methodName=SceneList&size=20&version=4.40&", _pageCount];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
    andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        // 加载
        if (_isRefresh == YES) {
            _pageCount++;
        }
        // 刷新
        else
        {
            [self.secDataArray removeAllObjects];
            self.pageCount = 1;
        }
        
        self.fllSceneModel = [FLLSceneModel modelWithDic:result];
        
        // 加载
        if (_isRefresh == YES) {
            [self.secDataArray addObjectsFromArray:(NSMutableArray *)self.fllSceneModel.data.data];
        }
        // 刷新
        else
        {
            self.secDataArray = (NSMutableArray *)self.fllSceneModel.data.data;
            
        }

        [self.sceneCollectionView reloadData];
             } andFailureBlock:^(NSError *error) {
                 
            }];
}


- (void)createSceneCollectionView
{
    UICollectionViewFlowLayout *sceneLayout = [[UICollectionViewFlowLayout alloc] init];
    sceneLayout.minimumInteritemSpacing = CellMargin;
    sceneLayout.minimumLineSpacing = CellMargin;
    sceneLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.sceneCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:sceneLayout];
    [self.view addSubview:_sceneCollectionView];
    [_sceneCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(0);
    }];
    _sceneCollectionView.delegate = self;
    _sceneCollectionView.dataSource = self;
    _sceneCollectionView.backgroundColor = FLLWhiteColor;
    [_sceneCollectionView registerNib:[UINib nibWithNibName:@"FLLSceneCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"sceneCollectionCell"];
    
    // 下拉刷新
    self.sceneCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self createPOSTRequest];
        [self.sceneCollectionView.mj_header endRefreshing];
    }];
   
    // 上拉加载
    self.sceneCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       
        self.isRefresh = YES;
        [self createPOSTRequest];
        [self.sceneCollectionView.mj_footer endRefreshing];
        
    }];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.secDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLSceneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"sceneCollectionCell" forIndexPath:indexPath];
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_secDataArray[indexPath.row].scene_background] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.titleLabel.text = _secDataArray[indexPath.row].scene_title;

    // 下面将NSNumber转成NSString类型
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    NSString *string = [numberFormatter stringFromNumber:_secDataArray[indexPath.row].dish_count];
    cell.dishCount.text = [NSString stringWithFormat:@"%@道菜", string];
    cell.titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    cell.dishCount.font = [UIFont systemFontOfSize:13 * kWidthScale];
    if (_secDataArray[indexPath.row].is_new == 0 ) {
        cell.hasIconImageView.image = [UIImage new];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat itemWidth = (_sceneCollectionView.bounds.size.width - 2 * CellMargin) / 3.0;
    CGFloat itemHeight = itemWidth * 5 / 4.0;
    return CGSizeMake(itemWidth, itemHeight);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLSceneInfoViewController *sceneInfoVC = [[FLLSceneInfoViewController alloc] init];
    [self.navigationController pushViewController:sceneInfoVC animated:YES];
    sceneInfoVC.idString = (NSMutableString *)[NSString stringWithFormat:@"%zd", _secDataArray[indexPath.row].scene_id];
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
